import wx from 'we-jssdk';
import { DB } from '../config/db.js'; 
import Vue from 'vue';
const wxConfig = {
	init(store, name){

		wxConfig.tongjiEvents(store, name);

		// 获取微信jssdk配置
		// url去掉#后面的数据
		let url = encodeURIComponent(location.href.split('#')[0]);
		return Vue.http.post(DB.ip + 'activity/ticket', {
			url: url
		}).then((res) => {
			res = res.body;
			if(res.code == '000000'){
				const config = {
					debug: false,
					jsApiList: [
				    	'onMenuShareTimeline',
						'onMenuShareAppMessage',
						'getLocation',
						'hideOptionMenu',
						'showOptionMenu',
						'hideMenuItems',
						'showMenuItems',
						'hideAllNonBaseMenuItem',
						'showAllNonBaseMenuItem',
						'closeWindow'
				    ],
				    appId: res.data.appId,
				    nonceStr: res.data.nonceStr,
				    signature: res.data.signature,
				    timestamp: res.data.timestamp
				}

				wx.config(config);


				wx.ready(() => {
					wx.hideAllNonBaseMenuItem();

					wx.hideMenuItems({
						menuList: [
							'menuItem:refresh'
						]
					});

					wx.showMenuItems({
						menuList: [
							'menuItem:share:appMessage',
							'menuItem:share:timeline'
						]
					});

					this.wxShareSetting(store);
				});
			}
		}, (error) => {
		});
	},
	wxShareSetting(t){
		if(!t.state || !t.state.currentActivityInfo || !t.state.currentActivityInfo.current || !t.state.currentActivityInfo.current.id){
			wx.onMenuShareTimeline({
			    title: '疯狂豪车周周开抢！新客注册即送188元租车券！', // 分享标题
			    link: DB.sharedomain + '?channel=105', // 分享链接
			    imgUrl: 'https://carphoto.atzuche.com/web/images/300.jpg', // 分享图标
			    success(){},
			    cancel: function () { 
			        // 用户取消分享后执行的回调函数
			    }
			});

			wx.onMenuShareAppMessage({
			    title: '疯狂豪车周周开抢！新客注册即送188元租车券！', // 分享标题
			    desc: '万款豪车低价抢，奔驰宝马每周不重样！不拼RP，就拼手速！快戳！', // 分享描述
			    link: DB.sharedomain + '?channel=105', // 分享链接
			    imgUrl: 'https://carphoto.atzuche.com/web/images/300.jpg', // 分享图标
			    success(){},
			    cancel: function () { 
			        // 用户取消分享后执行的回调函数
			    }
			});

			return;
		}

		wx.onMenuShareTimeline({
		    title: t.state.currentActivityInfo.current.shareTitle || '疯狂豪车周周开抢！新客注册即送188元租车券！', // 分享标题
		    link: DB.sharedomain + '?channel=' + t.state.userSubscribeInfo.channel || '105', // 分享链接
		    imgUrl: DB.aliyundomain + t.state.currentActivityInfo.current.shareImgs || 'https://carphoto.atzuche.com/web/images/300.jpg', // 分享图标
		    success(){ 
		        // 用户确认分享后执行的回调函数
		        Vue.http.post(DB.ip + '/activity/share', {
					openid: t.state.userSubscribeInfo.openid,
					channel: t.state.userSubscribeInfo.channel,
					activityId: t.state.currentActivityInfo.current.id
				});
		    },
		    cancel: function () { 
		        // 用户取消分享后执行的回调函数
		    }
		});

		wx.onMenuShareAppMessage({
		    title: t.state.currentActivityInfo.current.shareTitle || '疯狂豪车周周开抢！新客注册即送188元租车券！', // 分享标题
		    desc: t.state.currentActivityInfo.current.shareText || '万款豪车低价抢，奔驰宝马每周不重样！不拼RP，就拼手速！快戳！', // 分享描述
		    link: DB.sharedomain + '?channel=' + t.state.userSubscribeInfo.channel || '105', // 分享链接
		    imgUrl: DB.aliyundomain + t.state.currentActivityInfo.current.shareImgs || 'https://carphoto.atzuche.com/web/images/300.jpg', // 分享图标
		    success(){ 
		        // 用户确认分享后执行的回调函数
		        Vue.http.post(DB.ip + '/activity/share', {
					openid: t.state.userSubscribeInfo.openid,
					channel: t.state.userSubscribeInfo.channel,
					activityId: t.state.currentActivityInfo.current.id
				});
		    },
		    cancel: function () { 
		        // 用户取消分享后执行的回调函数
		    }
		});
	},
	tongjiEvents(t, name, subname){
		// subname 用于记录活动页面的不同状态
		if(!t.state || !t.state.currentActivityInfo || !t.state.currentActivityInfo.current || !t.state.currentActivityInfo.current.id){return};

		if(name === 'activity'){
			name = subname? subname: 'index';
		}

		Vue.http.get(DB.ip + 'statistic/count', {
			params: {
				activityId: t.state.currentActivityInfo.current.id,
				viewNo: name, 
				openid: t.state.userSubscribeInfo.openid,
				channel: t.state.userSubscribeInfo.channel
			}
		});
	}
}


export default wxConfig;